var api = require('../../../config/api.js');
const util = require('../../../utils/util.js');

const app = getApp();
Page({
    data: {
        disabled: false,
        id: 0,
        employee_number: '',
        employee_name: '',
        employee_department: '',
        phone:'',
        sex: 0,
        errmsg:'',
        sexItem: [
          {value: 0, name: '男', checked: true},
          {value: 1, name: '女', checked: false},
        ],
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getNurseInfofromServer();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    bindEmployeeNumberInput: function(event) {
        console.log(event.detail);
        this.setData({
            employee_number: event.detail
        });
    },
    bindEmployeeNameInput: function(event) {
        console.log(event.detail);
        this.setData({
            employee_name: event.detail
        });
    },
    bindEmployeeDepartmentInput: function(event) {
        console.log(event.detail);
        this.setData({
            employee_department: event.detail
        });
    },
    bindEmployeeMobileNumberInput: function(event) {
        console.log(event.detail);
        this.setData({
            phone: event.detail
        });
    },
    sendNurseinfotoServer: function(wxCode) {
        let that = this;
    
        console.log('sendNurseinfotoServer = userId ' + app.globalData.userId
        + 'jobNumber ' + that.data.jobNumber
        + 'name = ' + that.data.employee_name
        + 'phone = ' + that.data.phone
        + 'department = ' + that.data.employee_department);

        wx.request({
          url: api.NurseInfoSubmit,
          data: {
            id: that.data.id,
            userId: app.globalData.userId,
            jobNumber: that.data.employee_number,
            name: that.data.employee_name,
            sex: that.data.sex,
            phone: that.data.phone,
            department: that.data.employee_department
          },
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success: function(res) {
            if (res.data.errno == 0) {
              let userInfo = wx.getStorageSync('userInfo');
              userInfo.role = 'nurse';
              app.globalData.role = 'nurse';

              userInfo.metaId = res.data.data.id;
              app.globalData.metaId = res.data.data.id;
              
              wx.setStorageSync('userInfo', userInfo);

              wx.showModal({
                title: '资料已提交，请等待审核',
                content: res.data.errmsg,
                showCancel: false,
                success (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                    wx.redirectTo({
                      url: '/pages/nurse/nursehome/nursehome',
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              });
            } else {
              wx.showModal({
                title: '错误信息',
                content: res.data.errmsg,
                showCancel: false
              });
            }
          }
        });
      },
    SubmitNurseInfo() {
        var that = this;
        that.setData({
          disabled: true
        });
        that.sendNurseinfotoServer();
    },
    getNurseInfofromServer: function(){
      let that = this;
      console.log("getNurseInfofromServer with url: " + api.NurseInfoList + ' app.globalData.metaId ' + app.globalData.metaId);
      util.request(api.NurseInfoList,{
        id: app.globalData.metaId
      }).then(function(res){
        console.log("getNurseInfofromServer" + JSON.stringify(res));
        var info_temp = res['data'];
        if(info_temp!=null && info_temp!=""){
          console.log("getNurseInfofromServer info_temp " + info_temp['name']);
          that.setData({
              id: info_temp['id'],
              employee_number: info_temp['jobNumber'],
              employee_name: info_temp['name'],
              employee_department: info_temp['department'],
              sex: info_temp['sex'],
              phone:info_temp['phone']
          });

          let sexItem = that.data.sexItem;
          for (let i = 0, len = sexItem.length; i < len; ++i) {
            console.log('query sex：' + sexItem[i].name + " that.data.sex " + that.data.sex)
            if(sexItem[i].value == that.data.sex) {
              sexItem[i].checked = true;
              sexItem[1-i].checked = false;
              that.setData({
                sexItem: sexItem
              });
              console.log('getNurseInfofromServer query sex set ：', sexItem[i].name + ' sexItem[i].checked = ' + sexItem[i].checked);
            }
          }

        }
      });
    },
    sexRadioChange(e) {
      console.log('sexRadioChange发生change事件，携带value值为：', e.detail.value)
  
      const sexItem = this.data.sexItem
      for (let i = 0, len = sexItem.length; i < len; ++i) {
        console.log('sexRadioChange发生change事件，sexItem[i].value ', sexItem[i].value)
        sexItem[i].checked = sexItem[i].value == e.detail.value
        console.log('sexRadioChange发生change事件，sexItem[i].checked值为：', sexItem[i].checked)
      }
  
      this.setData({
        sexItem
      })
  
      for (let i = 0, len = sexItem.length; i < len; ++i) {
        console.log('sexRadioChange发生change事件，sexItem[i].checked值为：', sexItem[i].checked)
        if(sexItem[i].checked) {
            this.setData({
              sex: sexItem[i].value
            })
        }
      }
    },
})